﻿//-----------------------------------------------------------------------
// <copyright>
// This file is subject to the terms and conditions of the Microsoft Public License (MS-PL).
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL for more details.
// </copyright>
//----------------------------------------------------------------------- 
using Fulcrum.ControlGenerator;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Fulcrum.UnitTests.ControlGenerator
{
    [TestClass]
    public class RoutedCommandBindingXmlConfigurationTests
    {
        public RoutedCommandBindingXmlConfigurationTests()
        {
            // nothing
        }

        const string StandardXml = "<blah name='my name' canExecuteAlways='true' summary='my summary' owningType='my owning type' ></blah>";
        const string EmptyNameXml = "<blah name='' canExecuteAlways='true' summary='my summary' owningType='my owning type' ></blah>";
        const string MissingNameXml = "<blah canExecuteAlways='true' summary='my summary' owningType='my owning type' ></blah>";
        const string EmptyCanExecuteAlwaysXml = "<blah name='my name' canExecuteAlways='' summary='my summary' owningType='my owning type' ></blah>";
        const string MissingCanExecuteAlwaysXml = "<blah name='my name' summary='my summary' owningType='my owning type' ></blah>";
        const string FalseCanExecuteAlwaysXml = "<blah name='my name' canExecuteAlways='false' summary='my summary' owningType='my owning type' ></blah>";
        const string EmptySummaryXml = "<blah name='my name' canExecuteAlways='true' summary='' owningType='my owning type' ></blah>";
        const string MissingSummaryXml = "<blah name='my name' canExecuteAlways='true' owningType='my owning type' ></blah>";
        const string EmptyOwningTypeXml = "<blah name='my name' canExecuteAlways='true' summary='' owningType='' ></blah>";
        const string MissingOwningTypeXml = "<blah name='my name' canExecuteAlways='true' ></blah>";

        [TestMethod]
        public void TestReaderAdvancement()
        {
            var xmlReader = XmlTestHelp.CreateXmlReader(StandardXml);

            Assert.IsTrue(xmlReader.IsStartElement());

            var config = CreateRoutedCommandBindingXmlConfiguration(xmlReader);

            Assert.IsFalse(xmlReader.IsStartElement());
        }

        [TestMethod]
        public void TestCanExecuteAlways()
        {
            var xmlReader = XmlTestHelp.CreateXmlReader(StandardXml);

            var config = CreateRoutedCommandBindingXmlConfiguration(xmlReader);
            Assert.IsTrue(config.CanExecuteAlways);
        }

        private static RoutedCommandBindingXmlConfiguration CreateRoutedCommandBindingXmlConfiguration(System.Xml.XmlReader xmlReader)
        {
            var config = new RoutedCommandBindingXmlConfiguration(xmlReader);
            return config;
        }

        [TestMethod]
        public void TestMissingCanExecuteAlways()
        {
            var xmlReader = XmlTestHelp.CreateXmlReader(MissingCanExecuteAlwaysXml);

            var config = CreateRoutedCommandBindingXmlConfiguration(xmlReader);
            Assert.IsTrue(config.CanExecuteAlways);
        }

        [TestMethod]
        public void TestEmptyCanExecuteAlways()
        {
            var xmlReader = XmlTestHelp.CreateXmlReader(EmptyCanExecuteAlwaysXml);

            var config = CreateRoutedCommandBindingXmlConfiguration(xmlReader);
            Assert.IsTrue(config.CanExecuteAlways);
        }

        [TestMethod]
        public void TestFalseCanExecuteAlways()
        {
            var xmlReader = XmlTestHelp.CreateXmlReader(FalseCanExecuteAlwaysXml);

            var config = CreateRoutedCommandBindingXmlConfiguration(xmlReader);
            Assert.IsFalse(config.CanExecuteAlways);
        }

        [TestMethod]
        public void TestName()
        {
            var xmlReader = XmlTestHelp.CreateXmlReader(StandardXml);

            var config = CreateRoutedCommandBindingXmlConfiguration(xmlReader);
            Assert.AreEqual("my name", config.Name);
        }

        [TestMethod]
        public void TestMissingName()
        {
            var xmlReader = XmlTestHelp.CreateXmlReader(MissingNameXml);

            var config = CreateRoutedCommandBindingXmlConfiguration(xmlReader);
            Assert.IsNull(config.Name);
        }

        [TestMethod]
        public void TestEmptyName()
        {
            var xmlReader = XmlTestHelp.CreateXmlReader(EmptyNameXml);

            var config = CreateRoutedCommandBindingXmlConfiguration(xmlReader);
            Assert.AreEqual("", config.Name);
        }

        [TestMethod]
        public void TestOwningType()
        {
            var xmlReader = XmlTestHelp.CreateXmlReader(StandardXml);

            var config = CreateRoutedCommandBindingXmlConfiguration(xmlReader);
            Assert.AreEqual("my owning type", config.OwningType);
        }

        [TestMethod]
        public void TestMissingOwningType()
        {
            var xmlReader = XmlTestHelp.CreateXmlReader(MissingOwningTypeXml);

            var config = CreateRoutedCommandBindingXmlConfiguration(xmlReader);
            Assert.IsNull(config.OwningType);
        }

        [TestMethod]
        public void TestEmptyOwningType()
        {
            var xmlReader = XmlTestHelp.CreateXmlReader(EmptyOwningTypeXml);

            var config = CreateRoutedCommandBindingXmlConfiguration(xmlReader);
            Assert.AreEqual("", config.OwningType);
        }

        [TestMethod]
        public void TestSummary()
        {
            var xmlReader = XmlTestHelp.CreateXmlReader(StandardXml);

            var config = CreateRoutedCommandBindingXmlConfiguration(xmlReader);
            Assert.AreEqual("my summary", config.Summary);
        }

        [TestMethod]
        public void TestMissingSummary()
        {
            var xmlReader = XmlTestHelp.CreateXmlReader(MissingSummaryXml);

            var config = CreateRoutedCommandBindingXmlConfiguration(xmlReader);
            Assert.IsNull(config.Summary);
        }

        [TestMethod]
        public void TestEmptySummary()
        {
            var xmlReader = XmlTestHelp.CreateXmlReader(EmptySummaryXml);

            var config = CreateRoutedCommandBindingXmlConfiguration(xmlReader);
            Assert.AreEqual("", config.Summary);
        }
    }
}
